import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

/**
 * 7. 扫雷
 * https://www.lanqiao.cn/problems/549/learning/?page=1&first_category_id=1&second_category_id=3&tag_relation=intersection&tags=%E7%9C%81%E8%B5%9B,2022&sort=pass_rate&asc=0
 */
public class Main7 {
    public static int[] dx = {-1,-1,-1,0,0,1,1,1};
    public static int[] dy = {-1,0,1,-1,1,-1,0,1};
    public static int[][] result;
    public static int[][] arr;
    public static int n,m;

    /**
     * 广度搜索
     * @param args
     */
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        //在此输入您的代码...
        n = scan.nextInt();
        m = scan.nextInt();
        arr = new int[n][m];
        result = new int[n][m];
        for(int i = 0;i < n;i++) {
            for(int j = 0;j < m;j++) {
                arr[i][j] = scan.nextInt();
            }
        }
        // 广度搜索
        bfs();
        for(int i = 0;i < n;i++) {
            for(int j = 0;j < m - 1;j++) {
                System.out.print(result[i][j] + " ");
            }
            System.out.println(result[i][m - 1]);
        }
        scan.close();
    }

    public static void bfs() {
        Queue<int[]> queue = new LinkedList<>();
        for(int i = 0;i < n;i++) {
            for(int j = 0;j < m;j++) {
                if(arr[i][j] == 1) {
                    result[i][j] = 9;
                    queue.add(new int[]{i,j});
                }
            }
        }

        while(!queue.isEmpty()) {
            int[] t = queue.poll();
            int a = t[0];
            int b = t[1];
            for(int k = 0;k < 8;k++) {
                int x = a + dx[k];
                int y = b + dy[k];
                if(x >= 0 && x < n && y >= 0 && y < m && arr[x][y] == 0) {
                    result[x][y]++;
                }
            }
        }
    }

    /**
     * 深度搜索
     * @param args
     */
    public static void main2(String[] args) {
        Scanner scan = new Scanner(System.in);
        //在此输入您的代码...
        n = scan.nextInt();
        m = scan.nextInt();
        arr = new int[n][m];
        result = new int[n][m];
        for(int i = 0;i < n;i++) {
            for(int j = 0;j < m;j++) {
                arr[i][j] = scan.nextInt();
            }
        }
        // 深度搜索
        dfs();
        for(int i = 0;i < n;i++) {
            for(int j = 0;j < m - 1;j++) {
                System.out.print(result[i][j] + " ");
            }
            System.out.println(result[i][m - 1]);
        }
        scan.close();
    }

    public static void dfs() {
        for(int i = 0;i < n;i++) {
            for(int j = 0;j < m;j++) {
                if(arr[i][j] == 1) {
                    result[i][j] = 9;
                }else {
                    for(int k = 0;k < 8;k++) {
                        int x = dx[k] + i;
                        int y = dy[k] + j;
                        if(x >= 0 && x < n && y >= 0 && y < m && arr[x][y] == 1) {
                            result[i][j]++;
                        }
                    }
                }
            }
        }
    }
}
